# -*- coding: utf-8 -*-
from __future__ import annotations

import threading
import time
from typing import List, Dict

from core import threadLocal
from core.dataClasses import Event, TradeInfo, TacticResultData, SecurityHolder, SecurityGetter
from core.enums import Period, EventType, DateFormat, TradeType, ThreadName
from core.securitySelection import SecuritySelectionMultiThread
from event.event_manager import EventManager
from tactic.ma5 import TradeMa5Tactic
from config.securities import *
from core import logger


class EventHandler:

    def __init__(self, lock: threading.Lock, shareDict: Dict[str, SecurityHolder]):
        self.lock: threading.Lock = lock
        self.shareDict: Dict[str, SecurityHolder] = shareDict

    def __call__(self, e: Event):
        data: TacticResultData = e.data.data
        with self.lock:
            if e.type in [EventType.TACTIC_MONITOR, EventType.TACTIC_UNMONITOR] and data.security not in self.shareDict[data.classify].ins:
                data.original = self.shareDict[data.sourceClassify].getInfo(data.security)
                self.shareDict[data.sourceClassify] = self.shareDict[data.sourceClassify].delSecurity(data)
                self.shareDict[data.classify] = self.shareDict[data.classify].addSecurity(data)


def seletion(sourceSecurities: List[str]):
    name = str(threading.current_thread().ident)
    threadLocal.add(ThreadName.EVENT_MANAGER.value, name)
    threadLocal.add(ThreadName.CLOCK.value, name)
    threadLocal.add(ThreadName.CONTEXT.value, name)
    lock = threading.Lock()

    shareDict = dict()
    noticeHolder = SecurityHolder()
    st_init = "sourceHolder"
    st_notice = "noticeHolder"
    shareDict[st_init] = SecurityHolder().addSecurities(sourceSecurities)
    shareDict[st_notice] = SecurityHolder()

    event_manager: EventManager = EventManager.getInstance(name=name)
    eventHandler = EventHandler(lock, shareDict)
    event_manager.addEventListener(eventType=EventType.TACTIC_MONITOR, handler=eventHandler)
    event_manager.addEventListener(eventType=EventType.TACTIC_UNMONITOR, handler=eventHandler)
    event_manager.start()

    def selectionThread(shareDict: Dict[str, SecurityHolder], name: str):
        threadLocal.add(ThreadName.EVENT_MANAGER.value, name)
        threadLocal.add(ThreadName.CLOCK.value, name)
        threadLocal.add(ThreadName.CONTEXT.value, name)
        tactic = TradeMa5Tactic(noticeclassify=st_notice, sourceClassify=st_init).setPeriod(period=Period.day)
        selection = SecuritySelectionMultiThread().setGetter(SecurityGetter(shareDict, st_init)).setTactic(tactic)
        selection.start()

    pPointDetecting = threading.Thread(target=selectionThread, args=(shareDict, name))
    pPointDetecting.start()

    while (True):
        logger.success(f"\n买卖信息：\n{str(shareDict[st_notice])}")
        time.sleep(10)


if __name__ == "__main__":
    securities = []
    securities = securities + sh50
    securities = securities + sz50
    securities = securities + hs300

    seletion(securities)